﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;

namespace Election.ToLapDSNguoiDiBau
{
    public partial class ToLapDanhSachNDB : Form
    {
        public ToLapDanhSachNDB()
        {
            InitializeComponent();
        }

        private void ToLapDanhSachNDB_Load(object sender, EventArgs e)
        {
            //load dot bau cu len combobox
            this.DSDotBauCucomboBox1.DataSource = orcl.ExecuteQuery("select madotbaucu from QUANLYBAUCU.DOTBAUCU");
            this.DSDotBauCucomboBox1.DisplayMember = "madotbaucu";
            this.DSDotBauCucomboBox1.ValueMember = "madotbaucu";


            if (int.Parse(this.DSDotBauCucomboBox1.Items.Count.ToString()) != 0)
            {
                //load du lieu len gridview danh sach chua chon :  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                DataTable table = new DataTable();
                table.Columns.Add("Mã Thành Viên");
                table.Columns.Add("Họ Tên");

                table.Columns.Add("Tình Trạng công tác");

                table.Columns.Add("Thuộc đơn vị");

                OracleCommand comm = new OracleCommand();

                comm.Connection = orcl.conn;
                comm.CommandType = CommandType.StoredProcedure;
                comm.CommandText = "QUANLYBAUCU.DANH_SACH_DI_BAU";
                comm.Parameters.Add("dotbau", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
                comm.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
                try
                {
                    OracleDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        //  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                        DataRow row = table.NewRow();
                        row[0] = reader["matv"];
                        row[1] = reader["ten"];

                        row[2] = reader["tinhtrangcongtac"];

                        row[3] = reader["thuocdonvi"];
                        table.Rows.Add(row);
                    }
                    table.AcceptChanges();
                    //this.DSChuaChondataGridView1.DataSource = table;
                    reader.Close();
                    reader.Dispose();
                    comm.Dispose();
                    this.DSChuaChondataGridView1.Rows.Clear();
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        this.DSChuaChondataGridView1.Rows.Add(new object[] { false,table.Rows[i][0].ToString(), table.Rows[i][1].ToString(), table.Rows[i][2].ToString(), table.Rows[i][3].ToString() });
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                //load du lieu len gridview danh sach đã chon :  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                DataTable tableDaChon = new DataTable();
                tableDaChon.Columns.Add("Mã NBB");
                tableDaChon.Columns.Add("Mã Đợt Bình Bầu");

                tableDaChon.Columns.Add("Mã Thành Viên");



                OracleCommand comm2 = new OracleCommand();

                comm2.Connection = orcl.conn;
                comm2.CommandType = CommandType.StoredProcedure;
                comm2.CommandText = "QUANLYBAUCU.DANH_SACH_DIBAU_DA_DUOC_CHON";
                comm2.Parameters.Add("dotbau", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
                comm2.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
                try
                {
                    OracleDataReader reader = comm2.ExecuteReader();
                    while (reader.Read())
                    {
                        //  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                        DataRow row = tableDaChon.NewRow();
                        row[0] = reader["manbb"];
                        row[1] = reader["madotbaucu"];

                        row[2] = reader["matv"];


                        tableDaChon.Rows.Add(row);
                    }
                    tableDaChon.AcceptChanges();
                    this.DSNDBDaChonlistView1.DataSource = tableDaChon;
                    reader.Close();
                    reader.Dispose();
                    comm.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }

        }



        private void DSDotBauCucomboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           // MessageBox.Show(this.DSDotBauCucomboBox1.Text);
            this.DSChuaChondataGridView1.Rows.Clear();
            if (int.Parse(this.DSDotBauCucomboBox1.Items.Count.ToString()) != 0)
            {
                //load du lieu len gridview danh sach chua chon :  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                DataTable table = new DataTable();
                table.Columns.Add("Mã Thành Viên");
                table.Columns.Add("Họ Tên");

                table.Columns.Add("Tình Trạng công tác");

                table.Columns.Add("Thuộc đơn vị");

                OracleCommand comm = new OracleCommand();

                comm.Connection = orcl.conn;
                comm.CommandType = CommandType.StoredProcedure;
                comm.CommandText = "QUANLYBAUCU.DANH_SACH_DI_BAU";


                /////////////

                comm.Parameters.Add("dotbau", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
                comm.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
                try
                {
                    OracleDataReader reader = comm.ExecuteReader();
                    while (reader.Read())
                    {
                        //  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                        DataRow row = table.NewRow();
                        row[0] = reader["matv"];
                        row[1] = reader["ten"];

                        row[2] = reader["tinhtrangcongtac"];

                        row[3] = reader["thuocdonvi"];
                        table.Rows.Add(row);
                    }
                    table.AcceptChanges();
                    //this.DSChuaChondataGridView1.DataSource = table;
                    
                    reader.Close();
                    reader.Dispose();
                    comm.Dispose();
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        this.DSChuaChondataGridView1.Rows.Add(new object[] { false, table.Rows[i][0].ToString(), table.Rows[i][1].ToString(), table.Rows[i][2].ToString(), table.Rows[i][3].ToString() });
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

                //load du lieu len gridview danh sach đã chon :  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                DataTable tableDaChon = new DataTable();
                tableDaChon.Columns.Add("Mã NBB");
                tableDaChon.Columns.Add("Mã Đợt Bình Bầu");

                tableDaChon.Columns.Add("Mã Thành Viên");



                OracleCommand comm2 = new OracleCommand();

                comm2.Connection = orcl.conn;
                comm2.CommandType = CommandType.StoredProcedure;
                comm2.CommandText = "QUANLYBAUCU.DANH_SACH_DIBAU_DA_DUOC_CHON";
                comm2.Parameters.Add("dotbau", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
                comm2.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
                try
                {
                    OracleDataReader reader = comm2.ExecuteReader();
                    while (reader.Read())
                    {
                        //  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                        DataRow row = tableDaChon.NewRow();
                        row[0] = reader["manbb"];
                        row[1] = reader["madotbaucu"];

                        row[2] = reader["matv"];


                        tableDaChon.Rows.Add(row);
                    }
                    tableDaChon.AcceptChanges();
                    this.DSNDBDaChonlistView1.DataSource = tableDaChon;
                    reader.Close();
                    reader.Dispose();
                    comm.Dispose();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }

            }
        }



      //   them nguoi di bau
     

        private void Huybutton1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ThemNguoiDiBaubutton1_Click_1(object sender, EventArgs e)
        {

            foreach (DataGridViewRow row in this.DSChuaChondataGridView1.Rows)
                {
                    if (!(bool)row.Cells[0].Value)
                    {
                        continue;
                    }
                    try
                    {
                        OracleCommand com = new OracleCommand();
                        com.Connection = orcl.conn;
                        com.CommandType = CommandType.StoredProcedure;
                        com.CommandText = " QUANLYBAUCU.INSERT_NGUOIBINHBAU";
                        com.Parameters.Add("nbb", OracleDbType.Varchar2).Value = row.Cells[1].Value.ToString() + orcl.user + this.DSDotBauCucomboBox1.Text;
                        com.Parameters.Add("dbc", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
                        com.Parameters.Add("tv", OracleDbType.Varchar2).Value = row.Cells[1].Value.ToString();

                        ///thuuuuuuuuuuuuuuuuuuuuu
                      //  MessageBox.Show(row.Cells[0].Value.ToString() + orcl.user + this.DSDotBauCucomboBox1.Text + row.Cells[0].Value.ToString());

                        com.ExecuteNonQuery();
                        com.CommandType = CommandType.Text;
                        com.CommandText = "commit";
                        com.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
               
            }
            //load du lieu len gridview danh sach chua chon :  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
            DataTable table = new DataTable();
            table.Columns.Add("Mã Thành Viên");
            table.Columns.Add("Họ Tên");

            table.Columns.Add("Tình Trạng công tác");

            table.Columns.Add("Thuộc đơn vị");

            OracleCommand comm = new OracleCommand();

            comm.Connection = orcl.conn;
            comm.CommandType = CommandType.StoredProcedure;
            comm.CommandText = "QUANLYBAUCU.DANH_SACH_DI_BAU";


            /////////////

            comm.Parameters.Add("dotbau", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
            comm.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
            try
            {
                OracleDataReader reader = comm.ExecuteReader();
                while (reader.Read())
                {
                    //  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                    DataRow row = table.NewRow();
                    row[0] = reader["matv"];
                    row[1] = reader["ten"];

                    row[2] = reader["tinhtrangcongtac"];

                    row[3] = reader["thuocdonvi"];
                    table.Rows.Add(row);
                }
                table.AcceptChanges();
                this.DSChuaChondataGridView1.Rows.Clear();
                //this.DSChuaChondataGridView1.DataSource = table;
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    this.DSChuaChondataGridView1.Rows.Add(new object[] { false, table.Rows[i][0].ToString(), table.Rows[i][1].ToString(), table.Rows[i][2].ToString(), table.Rows[i][3].ToString() });
                }
                reader.Close();
                reader.Dispose();
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            //load du lieu len gridview danh sach đã chon :  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
            DataTable tableDaChon = new DataTable();
            tableDaChon.Columns.Add("Mã NBB");
            tableDaChon.Columns.Add("Mã Đợt Bình Bầu");

            tableDaChon.Columns.Add("Mã Thành Viên");



            OracleCommand comm2 = new OracleCommand();

            comm2.Connection = orcl.conn;
            comm2.CommandType = CommandType.StoredProcedure;
            comm2.CommandText = "QUANLYBAUCU.DANH_SACH_DIBAU_DA_DUOC_CHON";
            comm2.Parameters.Add("dotbau", OracleDbType.Varchar2).Value = this.DSDotBauCucomboBox1.Text;
            comm2.Parameters.Add("p_cursor", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
            try
            {
                OracleDataReader reader = comm2.ExecuteReader();
                while (reader.Read())
                {
                    //  matv, ten,  matb, tinhtrangcongtac, chucvu, thuocdonvi
                    DataRow row = tableDaChon.NewRow();
                    row[0] = reader["manbb"];
                    row[1] = reader["madotbaucu"];

                    row[2] = reader["matv"];


                    tableDaChon.Rows.Add(row);
                }
                tableDaChon.AcceptChanges();
                this.DSNDBDaChonlistView1.DataSource = tableDaChon;
                reader.Close();
                reader.Dispose();
                comm.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void masterCBox_CheckedChanged(object sender, EventArgs e)
        {
            if (this.masterCBox.Checked)
            {
                for (int i = 0; i < this.DSChuaChondataGridView1.Rows.Count; i++)
                {
                    this.DSChuaChondataGridView1.Rows[i].Cells[0].Value = true;
                }
            }
            else
            {
                for (int i = 0; i < this.DSChuaChondataGridView1.Rows.Count; i++)
                {
                    this.DSChuaChondataGridView1.Rows[i].Cells[0].Value = false;
                }
            }
        }


    }
}
